<?php

namespace Evaluating\Controller;

use Common\Controller\BaseController;
use Think\Page;

class IndexController extends BaseController
{
    public function index()
    {
        $task_type = 2;
        $model = D('VUserTask');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_POST['search_key'];
        $status = $_POST['search_status'];

        $this->assign('task_type', $task_type);

        $user_id = $this->current_user['id'];
        $list = $model->get_list($user_id, $search_key, $status, $task_type, null, $sp, C('PAGE_SIZE'));

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('task_list', $list[0]);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }
        $this->display();
    }

    public function detail2()
    {
        $id = $_GET['id'];
        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($id);
        $this->assign('user_task', $user_task);

        $usertaskquotamodel = D('VUserTaskQuota');
        $user_task_quota_list = $usertaskquotamodel->get_list($user_task['id'], null);

        $namelist = array();
        $contentlist = array();
        $quotalist = array();
        foreach ($user_task_quota_list as $quota) {
            $flag = false;
            $content_id = $quota['content_id'];
            foreach ($contentlist as $content_key => &$content_value) {
                if ($content_key == $content_id) {
                    $flag = true;
                    $content_value[] = $quota;
                }
            }
            if (!$flag) {
                $row = array();
                $row[] = $quota;
                $contentlist[$content_id] = $row;
            }
        }

        foreach ($user_task_quota_list as $quota) {
            $flag = false;
            $name_id = $quota['name_id'];
            foreach ($namelist as $name_key => &$name_value) {
                if ($name_key == $name_id) {
                    $flag = true;
                    $name_value[] = $quota;
                }
            }
            if (!$flag) {
                $row = array();
                $row[] = $quota;
                $namelist[$name_id] = $row;
            }
        }

        dump($contentlist);
        exit();
//        $this->assign('user_task_quota_list', $user_task_quota_list);
//        $this->display();
    }

    public function detail()
    {
        $id = $_GET['id'];
        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($id);
        $this->assign('user_task', $user_task);

        $usertaskquotamodel = D('VUserTaskQuota');
        $user_task_quota_list = $usertaskquotamodel->get_list($user_task['id'], null);

        $this->assign('user_task_quota_list', $user_task_quota_list);
        $this->display();
    }

    public function edit()
    {
        $id = $_GET['id'];
        $usertaskquotamodel = D('VUserTaskQuota');
        $user_task_quota = $usertaskquotamodel->get($id);
        $this->assign('user_task_quota', $user_task_quota);

        $usertaskmodel = D('VUserTask');
        $user_task = $usertaskmodel->get($user_task_quota['user_task_id']);
        $this->assign('user_task', $user_task);

        $usertaskannexmodel = D('UserTaskAnnex');
        $condition = array();
        $condition['user_task_quota_id'] = array('eq', $id);
        $annex_list = $usertaskannexmodel->where($condition)->order('id asc')->select();
        $this->assign('annex_list', $annex_list);
        $this->display();
    }

    public function save()
    {
        $model = D('UserTaskQuota');
        $data = $model->create();

        $data['report_at'] = time();

        $result = $model->save($data);

        if (!$result && $result != '') {
            echo $model->getError();
        } else {
            $annex_name = $_POST['annex_name'];
            $annex_path = $_POST['annex_path'];

            $cmodel = D('UserTaskAnnex');
            for ($i = 0; $i < count($annex_name); $i++) {
                if (!empty($annex_name[$i])) {
                    $cdata = $cmodel->create();
                    $name = $annex_name[$i];
                    $ename = end(explode(".", $name));
                    $atype = 0;
                    if (strtolower($ename) == 'doc') {
                        $atype = 1;
                    } else {
                        $atype = 2;
                    }
                    $cdata['id'] = null;
                    $cdata['annex_name'] = $name;
                    $cdata['annex_path'] = $annex_path[$i];
                    $cdata['annex_type'] = $atype;
                    $cdata['extension'] = $ename;
                    $cdata['user_task_id'] = $data['user_task_id'];
                    $cdata['user_task_quota_id'] = $data['id'];
                    $cmodel->add($cdata);
                }
            }
        }

        redirect('/evaluating/index/detail?id=' . $data['user_task_id']);
    }

    public function publish()
    {
        $user_task_id = $_POST['user_task_id'];
        $model = D('UserTask');

        $result = $model->where('id=%d', $user_task_id)->setField('status', 2);

        if (!$result && $result != '') {
            echo $model->getError();
        }

        redirect('/evaluating/index/index');
    }

    public function deleteannex()
    {
        $id = $_POST['id'];

        $usertaskannexmodel = D('UserTaskAnnex');

        $result = $usertaskannexmodel->delete($id);
        if (!$result && $result != '') {
            echo $usertaskannexmodel->getError();
        } else {
            echo 0;
        }
    }
}
